package com.ncst.mvcweb.service.impl;

import com.ncst.mvcweb.dao.OrderItemDao;
import com.ncst.mvcweb.dao.ProductOrderDao;
import com.ncst.mvcweb.dao.ProductOrderPageDao;
import com.ncst.mvcweb.entity.OrderItem;
import com.ncst.mvcweb.entity.ProductOrder;
import com.ncst.mvcweb.entity.User;
import com.ncst.mvcweb.service.ProductOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;

@Service
@Transactional
public class ProductOrderServiceImpl implements ProductOrderService {
    @Autowired
    private ProductOrderPageDao productOrderPageDao;
    @Autowired
    private ProductOrderDao productOrderDao;
    @Autowired
    private OrderItemDao orderItemDao;
    @Override
    public Page<ProductOrder> findByUser(User user, Pageable pageable) {
        return productOrderPageDao.findProductOrdersByOrderUser(user, pageable);
    }
    @Override
    public Page<ProductOrder> findByPage(Pageable pageable) {
        return productOrderDao.findAll(pageable);
    }
    @Override
    public ProductOrder queryById(Integer id) {
        return productOrderDao.findById(id).orElse(null);
    }

    @Override
    public ProductOrder save(ProductOrder order, List<OrderItem> orderItems) {
        productOrderDao.save(order);
        orderItemDao.saveAll(orderItems);
        return order;
    }

    @Override
    public List<ProductOrder> findAll() {
        return productOrderDao.findAll();
    }
}

